﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XTYAPI
{
    public class 数据排序
    {
        public static int[] 排序(int[] data, out int count, bool 升序 = true)
        {
            count = 0;
            for (int i = 0; i < data.Length - 1; i++)//控制比较次数
            {
                for (int j = 0; j < data.Length - 1 - i; j++)
                {
                    count++;
                    if (升序)
                    {
                        if (data[j] > data[j + 1])
                        {
                            data[j] = data[j] + data[j + 1];
                            data[j + 1] = data[j] - data[j + 1];
                            data[j] = data[j] - data[j + 1];
                        }

                    }
                    else
                    {
                        if (data[j] < data[j + 1])
                        {
                            data[j] = data[j] + data[j + 1];
                            data[j + 1] = data[j] - data[j + 1];
                            data[j] = data[j] - data[j + 1];
                        }
                    }
                }
            }
            return data;
        }
        public static double[] 排序(double[] data, out int count, bool 升序 = true)
        {
            count = 0;
            double temp = 0;
            for (int i = 0; i < data.Length - 1; i++)//控制比较次数
            {
                for (int j = 0; j < data.Length - 1 - i; j++)
                {
                    count++;
                    if (升序)
                    {
                        if (data[j] > data[j + 1])
                        {
                            temp = data[j];
                            data[j] = data[j + 1];
                            data[j + 1] = temp;
                        }
                    }
                    else
                    {
                        if (data[j] < data[j + 1])
                        {
                            temp = data[j];
                            data[j] = data[j + 1];
                            data[j + 1] = temp;
                        }
                    }
                }
            }
            return data;
        }
    }
}
